package com.example.weli.ble2test;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class FileOpenerDialog extends Dialog implements OnClickListener {
    public static final int TypeOpen = 1;
    public static final int TypeSave = 2;
    ArrayAdapter<String> Adapter;
    Runnable add = new Runnable() {
        public void run() {
            FileOpenerDialog.this.arr.clear();
            List<String> temp = FileOpenerDialog.this.getDirs(FileOpenerDialog.this.path);
            for (int i = 0; i < temp.size(); i += FileOpenerDialog.TypeOpen) {
                FileOpenerDialog.this.arr.add(temp.get(i));
            }
            FileOpenerDialog.this.Adapter.notifyDataSetChanged();
        }
    };
    ArrayList<String> arr = new ArrayList();
    private Button back;
    private Button cancel;
    Context context;
    private TextView curFilePath;
    private String[] fileType = null;
    private Button home;
    private LinearLayout layout;
    private ListView list;
    private MyDialogListener listener;
    private OnItemClickListener lvLis = new OnItemClickListener() {
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
            String temp = (String) arg0.getItemAtPosition(arg2);
            if (temp.equals("\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd\u04bb\ufffd\ufffd")) {
                FileOpenerDialog.this.path = FileOpenerDialog.this.getSubDir(FileOpenerDialog.this.path);
            } else if (FileOpenerDialog.this.path.equals("/")) {
                FileOpenerDialog.this.path = FileOpenerDialog.this.path + temp;
            } else {
                FileOpenerDialog.this.path = FileOpenerDialog.this.path + "/" + temp;
                if (new File(FileOpenerDialog.this.path).isFile()) {
                    FileOpenerDialog.this.dismiss();
                    FileOpenerDialog.this.listener.OnOkClick(FileOpenerDialog.this.path);
                }
            }
            if (FileOpenerDialog.this.type == FileOpenerDialog.TypeOpen) {
                FileOpenerDialog.this.curFilePath.setText("Path:" + FileOpenerDialog.this.path);
            }
            new Handler().post(FileOpenerDialog.this.add);
        }
    };
    private Button ok;
    private String path;
    private EditText saveFileName;
    private int type = TypeOpen;

    public FileOpenerDialog(Context context, int type, String[] fileType, String resultPath, MyDialogListener listener) {
        super(context);
        this.context = context;
        this.type = type;
        this.fileType = fileType;
        this.path = resultPath;
        this.listener = listener;
    }

    public void dismiss() {
        super.dismiss();
    }

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_filechooser);
        this.path = getSDPath();
        this.arr = (ArrayList) getDirs(this.path);
        this.Adapter = new ArrayAdapter(this.context, 17367043, this.arr);
        this.list = (ListView) findViewById(R.id.FileChooserDirList);
        this.list.setAdapter(this.Adapter);
        this.list.setOnItemClickListener(this.lvLis);
        this.home = (Button) findViewById(R.id.FileChooserHomeBtn);
        this.home.setOnClickListener(this);
        this.back = (Button) findViewById(R.id.FileChooserBackBtn);
        this.back.setOnClickListener(this);
        this.ok = (Button) findViewById(R.id.FileChooserOkBtn);
        this.ok.setOnClickListener(this);
        this.cancel = (Button) findViewById(R.id.FileChooserCancelBtn);
        this.cancel.setOnClickListener(this);
        this.layout = (LinearLayout) findViewById(R.id.FileChooserDirLayout);
        if (this.type == TypeOpen) {
            this.curFilePath = new TextView(this.context);
            this.layout.addView(this.curFilePath);
            this.curFilePath.setText("Path:" + this.path);
        } else if (this.type == TypeSave) {
            this.saveFileName = new EditText(this.context);
            this.saveFileName.setWidth(240);
            this.saveFileName.setHeight(70);
            this.saveFileName.setGravity(17);
            this.saveFileName.setPadding(0, TypeSave, 0, 0);
            this.layout.addView(this.saveFileName);
            this.saveFileName.setText("Enter file name");
        }
    }

    private List<String> getDirs(String ipath) {
        List<String> dirs = new ArrayList();
        File[] files = new File(ipath).listFiles();
        if (files != null) {
            int length = files.length;
            for (int i = 0; i < length; i += TypeOpen) {
                File f = files[i];
                if (f.isDirectory()) {
                    String tmp = f.toString();
                    if (tmp.endsWith("/")) {
                        tmp = tmp.substring(0, tmp.length() - 1);
                    }
                    dirs.add(tmp.substring(tmp.lastIndexOf("/") + TypeOpen, tmp.length()));
                } else if (f.isFile()) {
                    dirs.add(f.toString().substring(this.path.length() + TypeOpen, f.toString().length()));
                }
            }
        }
        if (dirs.size() == 0) {
            dirs.add("\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd\u04bb\ufffd\ufffd");
        }
        return dirs;
    }

    public void onClick(View args0) {
        if (args0.getId() == this.home.getId()) {
            this.path = getRootDir();
            if (this.type == TypeOpen) {
                this.curFilePath.setText("Path:" + this.path);
            }
            new Handler().post(this.add);
        } else if (args0.getId() == this.back.getId()) {
            this.path = getSubDir(this.path);
            if (this.type == TypeOpen) {
                this.curFilePath.setText("Path:" + this.path);
            }
            new Handler().post(this.add);
        } else if (args0.getId() == this.ok.getId()) {
            dismiss();
            this.listener.OnOkClick(this.path);
        } else if (args0.getId() == this.cancel.getId()) {
            cancel();
        }
    }

    private String getSDPath() {
        File sdDir = null;
        if (Environment.getExternalStorageState().equals("mounted")) {
            sdDir = Environment.getExternalStorageDirectory();
        }
        if (sdDir == null) {
            return "/";
        }
        return sdDir.toString();
    }

    private String getRootDir() {
        return "/";
    }

    private String getSubDir(String path) {
        String subpath = "/";
        if (path.endsWith("/")) {
            path = path.substring(0, path.length() - 1);
        }
        int pos = path.lastIndexOf("/");
        if (pos > 0) {
            return path.substring(0, pos);
        }
        return subpath;
    }
}
